package guoanmaker.operator.business.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.service.OperatorActiveTaskServiceInterface;
import guoanmaker.operator.business.service.OperatorActiveTaskTypeServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.OPERATOR_ACTIVE_TASK)
public class OperatorActiveTaskController {

	@Resource
	private OperatorActiveTaskServiceInterface operatorActiveTaskService;
	@Resource
	private OperatorActiveTaskTypeServiceInterface operatorActiveTaskTypeService;
	
	@Resource
	private AjaxJson ajaxJson;
	
	@ApiOperation(value = "查询任务可选则的活动", notes = "查询活动列表")
	@RequestMapping(value = "findOperatorEventDetails", method = RequestMethod.POST)
	public AjaxJson findOperatorEventDetails(){
		try{
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorActiveTaskService.findOperatorEventDetailsByAvailabilityAndActivityTypeIdAndLuckName("1", "抽奖", ""));
		}catch(Exception e){
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(null);
		}
		return ajaxJson;
	}
	
	/**
	 * @description:查询所有任务类型记录
	 * @return AjaxJson
	 */
	@ApiOperation(value = "查询所有任务类型", notes = "查询任务类型列表")
	@RequestMapping(value = "findAllOperatorActiveTaskType", method = RequestMethod.POST)
	public AjaxJson findAllOperatorActiveTaskType(){
		try{
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorActiveTaskTypeService.findAll());
		}catch(Exception e){
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(null);
		}
		return ajaxJson;
	}
	
	/**
	 * @description:新增或修改任务记录
	 * @param taskId 任务id
	 * @param taskName 任务名称
	 * @param taskTypeId 任务类型id
	 * @param taskAmount 订单金额
	 * @param activeId 活动id
	 * @return AjaxJson
	 */
	@ApiOperation(value = "新增或修改任务记录", notes = "新增或修改任务记录")
	@RequestMapping(value = "saveOrUpdateOperatorActiveTask", method = RequestMethod.POST)
	public AjaxJson saveOrUpdateOperatorActiveTask(String taskId, String taskName, String taskTypeId, String taskAmount, String activeId){
		try{
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorActiveTaskService.saveOrUpdateOperatorActiveTask(taskId, taskName, taskTypeId, taskAmount, activeId));
		}catch(Exception e){
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(null);
		}
		return ajaxJson;
	}
	
	/**
	 * @description:删除任务记录
	 * @param taskId 任务id
	 * @return AjaxJson
	 */
	@ApiOperation(value = "删除任务记录", notes = "根据任务id删除任务记录")
	@RequestMapping(value = "deleteOperatorActiveTask", method = RequestMethod.POST)
	public AjaxJson deleteOperatorActiveTask(String taskId){
		try{
			operatorActiveTaskService.deleteOperatorActiveTaskById(taskId);
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(null);
		}catch(Exception e){
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(null);
		}
		return ajaxJson;
	}
	
	/**
	 * @descrption:根据活动名称和活动类型分页查询
	 * @param taskName 任务名称
	 * @param taskTypeId 任务类型id
	 * @param page 当前页
	 * @param size 当前页显示记录的数量
	 * @return
	 */
	@ApiOperation(value = "分页查询任务记录", notes = "根据活动名称和活动类型分页查询任务记录")
	@RequestMapping(value = "findByTaskNameAndTaskType", method = RequestMethod.POST)
	public AjaxJson findByTaskNameAndTaskType(String taskName, String taskTypeId, String page,String size){
		try{
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorActiveTaskService.findByTaskNameAndTaskType(taskName, taskTypeId, page, size));
		}catch(Exception e){
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(null);
		}
		return ajaxJson;
	}
	
	/**
	 * @description:根据订单中的实付金额-优惠金额和任务类型id查询对应的活动url地址
	 * @param provideOrderFormId 订单id
	 * @param taskTypeId 任务类型id
	 * @return String-活动url地址
	 */
	@ApiOperation(value = "根据订单实付金额和任务id查询活动url,并添加或修改可抽奖次数", notes = "订单实付金额大于等于任务金额，并选择任务金额最大的任务所对应的活动id,根据活动类型拼成相应url地址,并添加或修改t_operator_active_chance")
	@RequestMapping(value = "findActiveURLByInfo", method = RequestMethod.POST)
	public AjaxJson findActiveURLByTruePrice(String provideOrderFormId, String taskTypeId){
		try{
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorActiveTaskService.findActiveURLByTruePriceAndTaskTypeId(provideOrderFormId, taskTypeId));
		}catch(Exception e){
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(null);
		}
		return ajaxJson;
	}
}
